package com.cly.house.dao.impl;

import java.util.List;

import com.cly.house.dao.DjDao;
import com.cly.house.entity.Emp;
import com.cly.house.entity.House;
import com.cly.house.entity.MyCus;
import com.cly.house.entity.Mydj;
import com.cly.house.uitl.JdbcUtil;
import com.cly.house.uitl.PageInfo;

public class DjDaoImpl implements DjDao {

	@Override
	public List<Mydj> listAll() {
		String sql = "SELECT * FROM mydj";
		List<Mydj> list=null;
		try {
			list = JdbcUtil.executeQuery(sql, Mydj.class);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int save(Mydj t) {
		String sql = "insert into mydj(cid,hid,myj,myzj,mdate,mimg,eid,mflag,mbegintime) values(?,?,?,?,?,?,?,?,?)";
		int count = JdbcUtil.executeUpdate(sql, t.getCid(),t.getHid(),t.getMyj(),t.getMyzj(),t.getMdate(),t.getMimg(),t.getEid(),t.getMflag(),t.getMbegintime());
		return count;
	}

	@Override
	public Mydj updateId(int id) {
		// TODO Auto-generated method stub
		Mydj mydj = null;
		String sql = "select * from mydj where mid=?";
		List<Mydj> list = null;
		try {
			list = JdbcUtil.executeQuery(sql, Mydj.class, id);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(list.size()>0) {
			mydj = list.get(0);
		}
		return mydj;
	}

	@Override
	public int update(Mydj t) {
		// TODO Auto-generated method stub
		String sql = "update mydj set cid=?,hid=?,mbegintime=?,myj=?,myzj=? where mid=?";
		int update = JdbcUtil.executeUpdate(sql, t.getCid(),t.getHid(),t.getMbegintime(),t.getMyj(),t.getMyzj(),t.getMid());
		
		return update;
	}

	@Override
	public boolean delete(int id) {
		String sql = "delete from mydj where mid=?";
		int i = JdbcUtil.executeUpdate(sql, id);
		if(i>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public PageInfo<Mydj> findByPage(int pageNum, int pageSize) {
		String sql = "select a.*,b.erealname,c.cname,c.ctel,d.haddress,d.hfh from mydj a inner join myemp b on "
		+ "a.eid=b.eid inner join mycus c on a.cid=c.cid inner join myhouse d on a.hid=d.hid";
		
		String sql1 = "select count(1) from mydj a inner join myemp b on a.eid=b.eid inner join mycus c on a.cid=c.cid inner join myhouse d on a.hid=d.hid";
				
		PageInfo<Mydj> pageInfo = new PageInfo<Mydj>();
		try {
			pageInfo.setList(JdbcUtil.findBypage(sql, pageNum, pageSize, Mydj.class));
			pageInfo.setPageNum(pageNum);
			pageInfo.setPageSize(pageSize);
			pageInfo.setTotal(JdbcUtil.total(sql1));
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageInfo;
	}
	
	

}
